﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NearForums.DataAccess.Xml
{
    class TemplatesDataAccess : BaseDataAccess, ITemplatesDataAccess
    {
        public void AddOrUpdate(Template template)
        {
            string filePath = GetDataPath(TemplatesFile);
            XDocument doc = XDocument.Load(filePath);
            if (template.Id == 0)
            {
                int count = (int)doc.Root.Attribute("Count");
                int templateId = ++count;
                XElement elem = new XElement("Template",
                                    new XAttribute("Id", templateId),
                                    new XAttribute("Key", template.Key),
                                    new XAttribute("Description", template.Description ?? string.Empty),
                                    new XAttribute("IsCurrent", template.IsCurrent),
                                    new XAttribute("Date", ToString(DateTime.Now)));
                doc.Root.Add(elem);
                doc.Root.SetAttributeValue("Count", count);
            }
            else
            {
                var qry = from t in doc.Root.Elements("Template")
                          where (int)t.Attribute("Id") == template.Id
                          select t;

                XElement elem = qry.SingleOrDefault();
                if (elem != null)
                {
                    elem.SetAttributeValue("Key", template.Key);
                    elem.SetAttributeValue("Description", template.Description);
                    elem.SetAttributeValue("IsCurrent", template.IsCurrent);
                }
            }
            
            doc.Save(filePath);
        }

        public void Delete(int id)
        {
            string filePath = GetDataPath(TemplatesFile);
            XDocument doc = XDocument.Load(filePath);
            
            var qry = from t in doc.Root.Elements("Template")
                       where (int)t.Attribute("Id") == id
                       select t;
            XElement elem = qry.SingleOrDefault();
            if (elem != null)
            {
                elem.Remove();
                doc.Save(filePath);
            }
        }

        public Template Get(int id)
        {
            string filePath = GetDataPath(TemplatesFile);
            XDocument doc = XDocument.Load(filePath);
            var qry = from t in doc.Root.Elements("Template")
                      where (int)t.Attribute("Id") == id
                      select t;
            var elem = qry.SingleOrDefault();
            Template template = null;
            if (elem != null)
                template = GetTemplateFromElement(elem);
            return template;
        }

        public List<Template> GetAll()
        {
            string filePath = GetDataPath(TemplatesFile);
            XDocument doc = XDocument.Load(filePath);
            var qry = from t in doc.Root.Elements("Template")
                      select t;
            List<Template> templates = new List<Template>();
            foreach (var elem in qry)
            {
                templates.Add(GetTemplateFromElement(elem));
            }
            return templates;
        }

        private Template GetTemplateFromElement(XElement elem)
        {
            Template t = new Template();
            t.Id = (int)elem.Attribute("Id");
            t.Key = (string)elem.Attribute("Key");
            t.Description = (string)elem.Attribute("Description");
            t.IsCurrent = (bool)elem.Attribute("IsCurrent");
            return t;
        }

        public Template GetCurrent()
        {
            string filePath = GetDataPath(TemplatesFile);
            XDocument doc = XDocument.Load(filePath);
            var qry = from t in doc.Root.Elements("Template")
                      where (bool)t.Attribute("IsCurrent") == true
                      select t;
            XElement elem = qry.SingleOrDefault();
            if (elem != null)
            {
                return GetTemplateFromElement(elem);
            }
            return null;
        }

        public void SetCurrent(int id)
        {
            string filePath = GetDataPath(TemplatesFile);
            XDocument doc = XDocument.Load(filePath);

            var qry1 = from t in doc.Root.Elements("Template")
                      where (bool)t.Attribute("IsCurrent") == true
                      select t;
            XElement elem1 = qry1.SingleOrDefault();
            if (elem1 != null)
            {
                elem1.SetAttributeValue("IsCurrent", false);
            }

            var qry2 = from t in doc.Root.Elements("Template")
                      where (int)t.Attribute("Id") == id
                      select t;
            XElement elem2 = qry2.SingleOrDefault();
            if (elem2 != null)
            {
                elem2.SetAttributeValue("IsCurrent", true);
            }

            doc.Save(filePath);
        }
    }
}
